﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e.Store.Domain;
using e.Store.Model.Entities;
using e.Store.Domain.Repositories;

namespace e.Store.Web.Controllers
{
    public class InventoryController : Controller
    {

        IUnitOfWork _IUWork;

        public InventoryController()
        {
            _IUWork = new UnitOfWorkcs();
        }

        public InventoryController(IUnitOfWork _IUnit)
        {
            _IUWork = _IUnit;
        }

        //
        // GET: /Inventory/

        public ActionResult Index()
        {
            return View();
        }


        #region PRODUCT MANAGEMENT

        // Products
        public ActionResult Products()
        {
            return View();
        }

        //Ass Product
        public ActionResult AddProduct()
        {
            List<Category> lstCategory = _IUWork.Repository<Category>().Query().Get().Where(c => c.Active == true).OrderBy(c => c.CategoryName).ToList();

            ViewBag.CategoryList = lstCategory;

            List<Supplier> lstSupplier = _IUWork.Repository<Supplier>().Query().Get().Where(c => c.Active == true).OrderBy(c => c.SupplierName).ToList();

            ViewBag.SupplierList = lstSupplier;

            return View("ProductCrud");
        }

        //Edit Product
        public ActionResult EditProduct(long id)
        {
            List<Category> lstCategory = _IUWork.Repository<Category>().Query().Get().Where(c => c.Active == true).OrderBy(c => c.CategoryName).ToList();

            ViewBag.CategoryList = lstCategory;

            List<Supplier> lstSupplier = _IUWork.Repository<Supplier>().Query().Get().Where(c => c.Active == true).OrderBy(c => c.SupplierName).ToList();

            ViewBag.SupplierList = lstSupplier;

            Product objPro = _IUWork.Repository<Product>().FindById(id);

            return View("ProductCrud", objPro);
        }

        [HttpPost]
        public ActionResult AddAndUpdateProduct(Product objProduct)
        {
            try
            {
                objProduct.CreatedBy = 1;
                objProduct.CreatedDate = DateTime.Now;

                if (objProduct != null && objProduct.ProductOID > 0)
                {

                    _IUWork.Repository<Product>().Update(objProduct);
                    _IUWork.Save();

                    TempData["Result"] = "Success!:Product updated successfully.";
                }
                else
                {
                    _IUWork.Repository<Product>().Insert(objProduct);
                    _IUWork.Save();

                    TempData["Result"] = "Success!:Product added successfully.";
                }
            }
            catch (Exception)
            {
                TempData["Result"] = "Error!:Please try later! An error has occurred while processing your request.";
            }

            return RedirectToAction("Products");
        }

        [HttpPost]
        public JsonResult CheckProduct(string productCode)
        {
            try
            {
                Product objPro = _IUWork.Repository<Product>().Query().Get().Where(c => c.ProductCode.Trim().ToLower() == productCode.Trim().ToLower()).FirstOrDefault();

                bool productExists = false;
                if (objPro != null)
                {
                    productExists = true;
                }
                return Json(new { Success = productExists, Message = "Product code already exists. Please use a unique code." }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { Success = false, Message = "Please try later! An error has occurred while processing your request." }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult LoadProductGridData()
        {
            ProductRepository proRepo = _IUWork.ExtRepositoryFor<ProductRepository>();

            List<LoadAllProductsResult> lstProduct = proRepo.LoadAllProducts();

            return PartialView("_ProductGrid", lstProduct);
        }

        #endregion


        #region SUPPLIER MANAGEMENT

        // Suppliers
        public ActionResult Supplier()
        {
            return View();
        }

        public ActionResult AddSupplier()
        {

            return View("SupplierCrud");
        }

        //Edit Supplier
        public ActionResult EditSupplier(long id)
        {
            Supplier objSup = _IUWork.Repository<Supplier>().Query().Get().Where(c => c.SupplierOID == id).FirstOrDefault();

            return View("SupplierCrud", objSup);
        }

        [HttpPost]
        public ActionResult AddAndUpdateSupplier(Supplier objSupplier)
        {
            try
            {
                if (objSupplier != null && objSupplier.SupplierOID > 0)
                {

                    _IUWork.Repository<Supplier>().Update(objSupplier);
                    _IUWork.Save();

                    TempData["Result"] = "Success!:Supplier updated successfully.";
                }
                else
                {
                    _IUWork.Repository<Supplier>().Insert(objSupplier);
                    _IUWork.Save();

                    TempData["Result"] = "Success!:Supplier added successfully.";
                }
            }
            catch (Exception)
            {
                TempData["Result"] = "Error!:Please try later! An error has occurred while processing your request.";
            }

            return RedirectToAction("Supplier");
        }

        [HttpPost]
        public JsonResult CheckSupplier(string supplierName)
        {
            try
            {
                Supplier objSup = _IUWork.Repository<Supplier>().Query().Get().Where(c => c.SupplierName.Trim().ToLower() == supplierName.Trim().ToLower()).FirstOrDefault();

                bool supplierExists = false;
                if (objSup != null)
                {
                    supplierExists = true;
                }
                return Json(new { Success = supplierExists, Message = "Supplier name already exists. Please use a unique name." }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { Success = false, Message = "Please try later! An error has occurred while processing your request." }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult LoadSupplierGridData()
        {

            List<Supplier> lstSupplier = _IUWork.Repository<Supplier>().Query().Get().ToList();

            return PartialView("_SupplierGrid", lstSupplier);
        }


        #endregion


        #region CAREGORY MANAGEMENT

        //Category
        public ActionResult Category()
        {
            return View();
        }

        public ActionResult AddCategory()
        {
            return View("CategoryCrud");
        }

        //Category
        public ActionResult EditCategory(long id)
        {
            Category objCat = _IUWork.Repository<Category>().Query().Get().Where(c => c.CategoryOID == id).FirstOrDefault();

            return View("CategoryCrud", objCat);
        }

        [HttpPost]
        public ActionResult AddAndUpdateCategory(Category objCategory)
        {
            try
            {
                if (objCategory != null && objCategory.CategoryOID > 0)
                {

                    _IUWork.Repository<Category>().Update(objCategory);
                    _IUWork.Save();

                    TempData["Result"] = "Success!:Category updated successfully.";
                }
                else
                {
                    _IUWork.Repository<Category>().Insert(objCategory);
                    _IUWork.Save();

                    TempData["Result"] = "Success!:Category added successfully.";
                }
            }
            catch (Exception)
            {
                TempData["Result"] = "Error!:Please try later! An error has occurred while processing your request.";
            }

            return RedirectToAction("Category");
        }

        [HttpPost]
        public JsonResult CheckCategory(string categoryName)
        {
            try
            {
                Category objCat = _IUWork.Repository<Category>().Query().Get().Where(c => c.CategoryName.Trim().ToLower() == categoryName.Trim().ToLower()).FirstOrDefault();

                bool categoryExists = false;
                if (objCat != null)
                {
                    categoryExists = true;
                }
                return Json(new { Success = categoryExists, Message = "Category name already exists. Please use a unique name." }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { Success = false, Message = "Please try later! An error has occurred while processing your request." }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult LoadCategoryGridData()
        {
            List<Category> lstCategory = _IUWork.Repository<Category>().Query().Get().ToList();

            return PartialView("_CategoryGrid", lstCategory);
        }

        public ActionResult LoadLeftCategoryMenu()
        {
            return PartialView("_LeftCategoryMenu");
        }

        #endregion
    }
}
